package com.by.and_widget.anim;

import android.animation.Animator;
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageView;

import java.io.Serializable;
import java.util.List;

/**
 * 查看大图动画效果帮助类
 */
public class LookPicAnimHelper {
    private static final long ANIM_TIME = 500L;
    private Interpolator mEnterInterpolator = new AccelerateInterpolator();
    private Interpolator mOuterInterpolator = new DecelerateInterpolator();

    private Activity mContext;
    private int enterPos;
    private List<BundleInfo> infos;
    private ImageView mImgView;
    private int locationX;
    private int locationY;
    private float scaleX;
    private float scaleY;

    public LookPicAnimHelper(Activity mContext, ImageView img) {
        this.mContext = mContext;
        mImgView = img;
    }

    public void bind(List<BundleInfo> data, int pos) {
        enterPos = pos;
        infos = data;
        if (infos == null || infos.size() == 0) {
            return;
        }
        runEnterAnimPre();
    }


    private void runEnterAnimPre() {
        if (mImgView != null) {
            mImgView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    mImgView.getViewTreeObserver().removeOnPreDrawListener(this);
                    runEnterAnim();
                    return true;
                }
            });
        }
    }

    /**
     * 执行进入时的动画
     */
    public void runEnterAnim() {
        BundleInfo info = infos.get(enterPos);
        int[] location = new int[2];
        mImgView.getLocationOnScreen(location);
        locationX = location[0];
        locationY = location[1];
        scaleX = 1.0f * info.getWidth() / mImgView.getWidth();
        scaleY = 1.0f * info.getHeight() / mImgView.getHeight();
        mImgView.setPivotX(0);
        mImgView.setPivotY(0);
        mImgView.setTranslationX(info.getLeft() - locationX);
        mImgView.setTranslationY(info.getTop() - locationY);
        mImgView.setScaleX(scaleX);
        mImgView.setScaleY(scaleY);
        mImgView.animate()
                .scaleX(1).scaleY(1)
                .translationX(0).translationY(0)
                .setInterpolator(mEnterInterpolator)
                .setDuration(ANIM_TIME).start();
    }


    public void runOuterAnim() {
        runOuterAnim(enterPos);
    }

    /**
     * 执行退出时的动画
     */
    public void runOuterAnim(int pos) {
        BundleInfo info = infos.get(pos);
        if (info == null) {
            mContext.finish();
            return;
        }
        scaleX = 1.0f * info.getWidth() / mImgView.getWidth();
        scaleY = 1.0f * info.getHeight() / mImgView.getHeight();
        mImgView.animate()
                .scaleX(scaleX).scaleY(scaleY)
                .translationX(info.getLeft() - locationX).translationY(info.getTop() - locationY)
                .setInterpolator(mOuterInterpolator)
                .setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        mContext.getWindow().getDecorView().setVisibility(View.GONE);
                        mContext.finish();
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {
                    }
                })
                .setDuration(ANIM_TIME).start();
    }

    /**
     * 保存位置信息
     */
    public static class BundleInfo implements Serializable {
        public int left;
        public int top;
        // ImageView显示的大小
        public int width;
        public int height;
        // 显示缩略图实际大小
        public int sourceWidth;
        public int sourceHeight;
        // 显示的图片链接
        public String url;


        public int getLeft() {
            return left;
        }

        public BundleInfo setLeft(int left) {
            this.left = left;
            return this;
        }

        public int getWidth() {
            return width;
        }

        public BundleInfo setWidth(int width) {
            this.width = width;
            return this;
        }

        public int getHeight() {
            return height;
        }

        public BundleInfo setHeight(int height) {
            this.height = height;
            return this;
        }

        public int getSourceWidth() {
            return sourceWidth;
        }

        public BundleInfo setSourceWidth(int sourceWidth) {
            this.sourceWidth = sourceWidth;
            return this;
        }

        public int getSourceHeight() {
            return sourceHeight;
        }

        public BundleInfo setSourceHeight(int sourceHeight) {
            this.sourceHeight = sourceHeight;
            return this;
        }

        public String getUrl() {
            return url;
        }

        public BundleInfo setUrl(String url) {
            this.url = url;
            return this;
        }

        public int getTop() {
            return top;
        }

        public BundleInfo setTop(int top) {
            this.top = top;
            return this;
        }
    }


}
